package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 智能设备Controller
 * 
 * @author zxy
 * @date 2025-05-30
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "智能设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询智能设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取智能设备列表")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('nursing:device:sync')")
    @PostMapping("/syncProductList")
    @ApiOperation(value = "新增智能设备")
    public R syncProductList(){

        deviceService.syncProductList();
         return R.ok();
    }
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/allProduct")
    @ApiOperation(value = "获取产品列表")
    public R getProductList(){
        List<ProductVo> productList = deviceService.getProductList();

        return R.ok(productList);
    }


    /**
     * 注册设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "智能设备", businessType = BusinessType.INSERT)
    @PostMapping("register")
    public R registerDevices(@RequestBody DeviceDto dto)
    {
        deviceService.registerDevices(dto);
        return R.ok();
    }


    /**
     * 查询设备详情
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping(value = "/{iotId}")
    public R<DeviceDetailVo> getDeicesInfo(@PathVariable("iotId") String iotId){

        DeviceDetailVo device = deviceService.queryDeviceByIotId(iotId);
        return R.ok(device);
    }



    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        AjaxResult ajaxResult = deviceService.queryServiceProperties(iotId);
        return ajaxResult;
    }

    /**
     * 修改设备数据
     *
     */
    @PutMapping()
    @ApiOperation("修改设备数据")
    public R updateDevice(@RequestBody DeviceDto device) {
        deviceService.updateDevice(device);
        return R.ok();
    }
}
